// pages/mine/mineOrder/mineOrder.js
var app = getApp();
import Api from '../../../api/api.js';
const api = new Api();
Page({

  /**
   * 页面的初始数据
   */
  data: {
    tapSelectIndex:0,
    tapList:["全部","待付款","已付款","待收货","待评价"],
    dataList:[],
    pageCount:10,
    pageIndex:1,
    isEmpty:false,
    isNeedFresh:false,
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    this.getListData();
  },
  tapItemClick(e){
    var index = e.currentTarget.dataset.key;
    this.setData({
      tapSelectIndex:index
    });
    this.data.pageIndex = 1;
    this.getListData();
  },
  listItemClick(e){
    // return;
    var orderId = e.currentTarget.dataset.key;
    wx.navigateTo({
      url: '/pages/mine/mineOrder/mineOrderDetail?orderId='+orderId,
    })
  },
  goToLookWiLiu(e){
    var orderId = e.currentTarget.dataset.key;
    wx.navigateTo({
      url: '/pages/mine/mineOrder/mineOrderDetail?orderId='+orderId,
    })
  },
  getListData() {
    var _this = this;
    var map = {};
    map.userId = app.USER_ID();
    map.appId = app.APP_ID;
    map.status = this.data.tapSelectIndex - 1;
    map.pageCount = this.data.pageCount;
    map.pageIndex = this.data.pageIndex;
    api.getRequestData(app.orderListUrl, map, true, "POST").then(res => {
      if (res.data.errorCode == 0) {
        var list = res.data.model.list;
        console.log(JSON.stringify(list[0]));
        if (list.length == 0) {
          app.alert("没有更多数据了～");
        }
        if (_this.data.pageIndex == 1) {
          _this.data.dataList = list;
          _this.setData({
            dataList: _this.data.dataList
          });
        } else {
          // 加载更多
          _this.setData({
            dataList: _this.data.dataList.concat(list)
          });
        }
        if (_this.data.dataList.length == 0){
          _this.setData({
            isEmpty: true
          });
        } else {
          _this.setData({
            isEmpty:false
          });
        }
      } else {
        wx.showToast({
          title: res.data.errorMsg,
          icon: 'none',
          duration: 1500
        });
      }
    });
  },
  statusItemClick(e){
    var index = parseInt(e.currentTarget.dataset.key);
    var dict = this.data.dataList[index];
    if (dict.status == 0) {
      // 继续支付
      this.goonPayOrder(dict);
    } else if (dict.status == 1) {

    } else if (dict.status == 2) {
      // 确认收货
      this.finishOrder(dict);
    } else if (dict.status == 3) {
      // 前往评价
      this.goToGoodsEvaluate(dict);
    }
  },
  finishOrder(dict){
    var _this = this;
    var map = {};
    map.userId = app.USER_ID();
    map.orderId = dict.orderId;
    api.getRequestData(app.orderFinishUrl, map, true, "POST").then(res => {
      if (res.data.errorCode == 0) {
        wx.showToast({
          title: "确认收货成功~",
          icon: 'success',
          mask: true,
          duration: 1500
        });
        setTimeout(() => {
          _this.data.pageIndex = 1;
          _this.getListData();
        }, 1400);
      } else {
        app.alert(res.data.errorMsg);
      }
    });
  },
  goToGoodsEvaluate(dict){
    var jsonString = JSON.stringify(dict);
    wx.setStorageSync('evaluateGoodsData', jsonString)
    var url = '/pages/mine/goodEvaluate/goodEvaluate';
    wx.navigateTo({
      url: url,
    })
  },
  // 重新支付
  goonPayOrder(dict){
    this.payOrder(dict.orderId);
  },
  payOrder(orderId) {
    var _this = this;
    var map = {};
    map.orderId = orderId;
    map.appId = app.APP_ID;
    map.userId = app.USER_ID();
    map.openId = app.OPEN_ID();
    api.getRequestData(app.payOrderUrl, map, false,"POST").then(res => {
      if (res.data.errorCode == 0) {
        var dict = res.data.model.wxPayMpOrderResult;
        _this.beginPay(dict, orderId);
      } else {
        app.alert(res.data.errorMsg);
      }
    }).catch(err => {
    });
  },
  beginPay: function (data, orderId) {
    var _this = this;
    wx.requestPayment({
      timeStamp: data.timeStamp,
      nonceStr: data.nonceStr,
      package: data.packageValue,
      signType: data.signType,
      paySign: data.paySign,
      success: function (res) {
        wx.showToast({
          title: "支付成功",
          icon: 'success',
          mask: true,
          duration: 1500
        });
        setTimeout(() => {
          _this.data.pageIndex = 1;
          _this.getListData();
        }, 1400);
      },
      fail: function (res) {
        app.alert("支付失败");
      },
      complete: function (res) {

      }
    })
  },
  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {
    if (this.data.isNeedFresh){
      this.data.isNeedFresh = false;
      this.data.pageIndex = 1;
      this.getListData();
    }
  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {
    this.data.pageIndex = 1;
    this.getListData();
  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {
    this.data.pageIndex += 1;
    this.getListData();
  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function () {

  }
})